package com.qc31.monitorlib.ui.carinfo

import android.content.Intent
import android.net.Uri
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import com.qc31.baselib.base.LazyFragment
import com.qc31.comlib.entity.GpsDetail
import com.qc31.monitorlib.R
import com.qc31.monitorlib.databinding.FragmentCarBaseBinding


/**
 *@Author Jasper
 *@Time   2021/2/27
 *@Desc   定位基础信息
 */
class CarBaseFragment : LazyFragment<FragmentCarBaseBinding>(FragmentCarBaseBinding::inflate) {

    private var driverPhone = ""
    private val mViewModel by activityViewModels<CarInfoViewModel>()
    override fun initView(view: View) {}

    override fun initData() {
        mViewModel.getGprR()
        mViewModel.locationLive.observe(this) {
            handleMsg(it)
        }
    }

    override fun initEvent() {
        binding.ivDriverPhone.setOnClickListener {
            if (driverPhone.isNotEmpty()) {
                val dialIntent =
                    Intent(Intent.ACTION_DIAL, Uri.parse("tel:$driverPhone")) //跳转到拨号界面，同时传递电话号码
                startActivity(dialIntent)
            }
        }
    }

    private fun handleMsg(entity: GpsDetail) {
        driverPhone = entity.drvPhone
        binding.lrTvPlateInfo.setRightText("${entity.carPlate}  ${entity.plateColor}")
        binding.lrTvCarName.setRightText(entity.carName)

        binding.lrTvServiceTime.setRightText(

            if (entity.expTime.isNullOrEmpty()) getString(R.string.desc_no_info) else entity.expTime!!
        )

        binding.lrTvCarTime.setRightText(entity.time)
        binding.lrTvCarACC.setRightText(entity.accTime)
        binding.lrTvCarState.setRightText("${entity.speed} km/h,${entity.drctCn} | ${entity.stateCn}")
        binding.lrTvOwner.setRightText(entity.owner?:"")
        binding.lrTvOwnerPhone.setRightText(entity.ownerPhone?:"")
        binding.lrTvCarTeam.setRightText(entity.teamName)
        binding.lrTvCarDriver.setRightText(
            entity.drvName.ifEmpty { requireContext().getString(R.string.desc_no_info) }
        )
        binding.ivDriverPhone.isVisible=driverPhone.isNotEmpty()
        binding.lrTvCarRun.setRightText(entity.runStopTime)
        binding.lrTvCarTotalMile.setRightText("${entity.mile} km")
        binding.lrTvCarDayMile.setRightText("${entity.preMile} km")
        binding.tvAddress.text = entity.addr?:""

        binding.lrTvCarComSignal.setRightText(entity.sgn)
        binding.lrTvCarSim.setRightText(entity.simCard)
        binding.lrTvCarLocation.setRightText(entity.satl)
        binding.lrTvCarTmnNum.setRightText(entity.tmnNo)
        binding.lrTvCarTmnKey.setRightText(entity.tmnKey ?: "")

        binding.lrTvCarTmnType.setRightText(entity.tmnType ?: "")
        binding.lrTvCarTmnTime.setRightText(entity.tmnExpirTime ?: "")
    }
}